<template>
    <el-row>
        <el-col :span="4" class="titleWidth">
            <div class="row-img"> 
                <img src="../../assets/logo.png">
                <h3>
                    亚健康服务平台
                </h3>
            </div>
        </el-col>
        <el-col :span="17">
            <div class="row-title">
            </div>
        </el-col>
        <el-col :span="3">
            <div class="row-drow">
                <el-dropdown>
                    <span class="el-dropdown-link">
                        <el-icon class="el-icon--left">
                            <Avatar />
                        </el-icon>
                            {{ username }}
                        <el-icon class="el-icon--right">
                            <arrow-down />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item @click="changePass">密码修改</el-dropdown-item>
                            <el-dropdown-item @click="logout">退出系统</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </div>
        </el-col>
    </el-row>
    <el-dialog v-model="dialogFormVisible" title="Shipping address">
        <el-form :model="form" :rules="rules" ref="form">
            <el-form-item prop="password0" label-width="200">
                <el-input
                    v-model="form.password0" 
                    type="password"
                    placeholder="请输入原密码"
                    autocomplete="off" />
            </el-form-item>
            <el-form-item prop="password1" label-width="200">
                <el-input 
                    v-model="form.password1"
                    type="password"
                    placeholder="请输入新密码"
                    autocomplete="off" />
            </el-form-item>
            <el-form-item prop="password2" label-width="200">
                <el-input 
                    v-model="form.password2"
                    type="password"
                    placeholder="请确认新密码"
                    autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="cancel">取消</el-button>
                <el-button type="primary" @click="savePass">确认</el-button>
            </span>
        </template>
  </el-dialog>
</template>

<script>
	
	import {changeState} from '../../request/api/login/login.js'
    import { changePass } from '../../request/api/home/index'
	
    export default {
        name: 'Head',
        components: {},
        data() {
            // 确认密码
		    let checkPW = ((rule, value, callback) => {
                if (value.trim().length === 0) {
                    callback(new Error('密码不能为空'))
                } else if (value !== this.form.password1) {
                    callback(new Error('密码不一致'))
                } else {
                    callback()
                }
            })
            return {
                username: '管理员',
                form: {
                    password0: '',
                    password1: '',
                    password2: ''
                },
                dialogFormVisible: false,
                rules: {
                    password0: [
                        {
                            required: true,
                            message: '请输入原密码',
                            trigger: 'blur'
                        },
                        {
                            min: 6,
                            max: 12,
                            message: '密码长度 6 ~ 12个字符',
                            trigger: 'blur'
                        }
                    ],
                    password1: [
                        {
                            required: true,
                            message: '请输入新密码',
                            trigger: 'blur'
                        },
                        {
                            min: 6,
                            max: 12,
                            message: '密码长度 6 ~ 12个字符',
                            trigger: 'blur'
                        }
                    ],
                    password2: [
                        {
                            validator: checkPW,
                            trigger: 'blur'
                        }
                    ],
                }
            }
        },
        created() {
            this.getUserName()
        },
        methods: {
            getUserName() {
                this.username = localStorage.getItem('username')
            },
            
            // 退出登录
            logout () {
				const obj = {
					id: localStorage.getItem('id'),
					state: false
				}
				changeState(obj).then(res => {
					if (res.data.code === 0) {
						// 清空token
						localStorage.clear()
                        this.$socket.disconnect()
                        this.sockets.unsubscribe("articleRemind")
						// 跳转到登录页
						this.$router.push('/login')
					}
				})
            },
            // 修改密码
            changePass() {
                this.dialogFormVisible = true
            },
            // 保存
            savePass() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        changePass({
                            userName: this.username,
                            password0: this.form.password0,
                            password1: this.form.password1
                        }).then(res => {
                            const {data} = res
                            if (data?.code === 0) {
                                this.$message({
									message: res.msg,
									type: 'success'
								})
                                this.cancel()
                                this.logout()
                            } else {
                                this.$message({
                                    message: res.msg,
                                    type: 'warning'
                                })
                                this.cancel()
                            }
                        }).catch(err => {
                            this.$message({
                                message: res.msg,
                                type: 'warning'
                            })
                            this.cancel()
                        })
                    } else {
                        return false
                    }
                })
            },
            // 取消
            cancel() {
                this.dialogFormVisible = false
                this.$refs.form.resetFields()
            }
        }
    }
</script>

<style lang="scss" scoped>
    :root {
      --el-color-primary: #409eff;
    }  
    .el-row { 
        border-bottom: 2px solid #F1F5F9;
        align-items: center;
        color: #666;
        background-color: #fff;
        .row-img {
            width:241px ;
            height: 60px;
            padding: 0 20px  0 5px;
            box-sizing: border-box;
            position: relative;
            display: flex;
            justify-content: space-evenly;
            align-items: center;
            img {
                width: 2rem;
                height: 2.2rem;
            }
            h3{
               letter-spacing: 2px;
            }
        }

        .row-drow {
            text-align: right;
            padding-right: 40px;
            box-sizing: border-box;
            .el-dropdown-link {
                cursor: pointer;
                color: #409eff;
                font-size: 16px;
                display: flex;
                align-items: center;
            }

            .el-icon-arrow-down {
                font-size: 16px;
            }
        }
    }
</style>
